package cn.com.dao;



import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.entity.Consumer;


//用户的dao文件
public class ConsumerDao {
	private SessionFactory sessionFac;
	
	public SessionFactory getSessionFac() {
		return sessionFac;
	}
	public void setSessionFac(SessionFactory sessionFac) {
		this.sessionFac = sessionFac;
	}
	Session session;
	Transaction tr;
	
	//注册增加
	public boolean addConsumer(Consumer consumer){
		boolean b=false;
		try {
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			
			session.save(consumer);
			tr.commit();
			session.close();
			b=true;
			return b;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
	//修改个人资料
	public boolean updateConsumer(Consumer consumer){
		boolean b=false;
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ctx.getBean("sessionFac");
		session=sessionFac.openSession();
		tr=session.beginTransaction();
		System.out.println("bbbbbbbbb:"+consumer);
		try {
			session.update(consumer);
			System.out.println("cccccccccc"+consumer);
			tr.commit();
			System.out.println("Dao有错么？");
			b=true;
			session.close();
			return b;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
	//验证用户和获得用户
	public Consumer Consumerlogin(Consumer consumer){
		
		Consumer loginconsumer=null;
		session=sessionFac.openSession();
		String hql="from Consumer where consumername=? and consumerpwd=?";
		Query query=session.createQuery(hql);
		query.setString(0, consumer.getConsumername());
		query.setString(1, consumer.getConsumerpwd());
		List<Consumer> list=query.list();
		if(list.size()==1){
			loginconsumer=list.get(0);//从session住获得对象
		}
		session.close();
		return loginconsumer;
	}
	//查询一个用户信息
	public Consumer selectOneConsumer(Consumer consumer){
		
		Consumer c=null;
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ctx.getBean("sessionFac");
		session=sessionFac.openSession();
		String hql="from Consumer where consumerid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0, consumer.getConsumerid());
		List<Consumer> list=query.list();
		if(list!=null){
			c=list.get(0);
		}
		session.close();
		return c;
		
	}
	
	
	//这个方法是通过用户名查询这个人
	/*public Consumer ConsumerOne(Consumer consumer){
		Consumer consumerone=null;
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ctx.getBean("sessionFac");
		session=sessionFac.openSession();
		String hql="from Consumer where consumername=?";
		Query query=session.createQuery(hql);
		query.setString(0, consumer.getConsumername());//
		List<Consumer> list=query.list();
		System.out.println(list.size());
		if(list!=null){
			consumerone=list.get(0);
		}
		System.out.println("到底有木有阿，，，，，，，出来呀。。。。。。。。。");
		session.close();
		return consumerone;
	}*/

}
